package org.ontoexplorer.ui.frame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import org.flexdock.docking.DockableFactory;
import org.flexdock.docking.DockingConstants;
import org.flexdock.docking.DockingManager;
import org.flexdock.docking.state.PersistenceException;
import org.flexdock.perspective.LayoutSequence;
import org.flexdock.perspective.Perspective;
import org.flexdock.perspective.PerspectiveFactory;
import org.flexdock.perspective.PerspectiveManager;
import org.flexdock.perspective.actions.OpenPerspectiveAction;
import org.flexdock.perspective.persist.FilePersistenceHandler;
import org.flexdock.perspective.persist.PersistenceHandler;
import org.flexdock.util.ResourceManager;
import org.flexdock.view.View;
import org.flexdock.view.Viewport;
import org.flexdock.view.actions.DefaultDisplayAction;
import org.ontoexplorer.ui.panel.explorer.ExplorerPanel;

public class ApplicationFrame extends JFrame implements DockingConstants{
	public static final String PERSPECTIVE_FILE = "PerspectiveDemo5.data";	
	private static final String EXPLORER_VIEW = "bird.view";
	private static final String DATASTORE_VIEW = "message.log";
	private static final String MAP_VIEW = "problem";
	private static final String CONSOLE_VIEW = "console";
	
	private static final String P1 = "p1";
	private static final String P2 = "p2";
	private static final String P3 = "p3";
			
	public ApplicationFrame() {
		super("OntoExplorer");		
		// setup the flexdock configuration
		configureDocking(this);
		
		setContentPane(createContentPane());
		setJMenuBar(createApplicationMenuBar());
	}	

	private JPanel createContentPane() {
		JPanel contentPane = new JPanel(new BorderLayout());
		contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

		//tworzymy glowny view port do dokowania
		Viewport viewport = new Viewport();
//        Border outerBorder = BorderFactory.createEmptyBorder(0,0,5,5);
//        Border innerBorder = new ShadowBorder();
//        viewport.setBorderManager(new StandardBorderManager(BorderFactory.createCompoundBorder(outerBorder, innerBorder)));
        
//        viewport.setBorder(new EmptyBorder(10, 10, 10, 10));

		//rejestrujemy glowny view port

		contentPane.add(viewport, BorderLayout.CENTER);
		return contentPane;
	}
	
	private JMenuBar createApplicationMenuBar() {
		JMenuBar menuBar = new JMenuBar();

		JMenu fileMenu = new JMenu("File");
		JMenu windowMenu = new JMenu("Window");
		JMenu helpMenu = new JMenu("Help");
		
		JMenu showViewMenu = new JMenu("Show View");

		showViewMenu.add(new DefaultDisplayAction(EXPLORER_VIEW));
		showViewMenu.add(new DefaultDisplayAction(DATASTORE_VIEW));
		showViewMenu.add(new DefaultDisplayAction(MAP_VIEW));
		showViewMenu.add(new DefaultDisplayAction(CONSOLE_VIEW));

		JMenu perspectiveMenu = new JMenu("Perspective Open");

		perspectiveMenu.add(new OpenPerspectiveAction(P1));
		perspectiveMenu.add(new OpenPerspectiveAction(P2));		
						
		windowMenu.add(showViewMenu);
		windowMenu.add(perspectiveMenu);
		
		menuBar.add(fileMenu);
		menuBar.add(windowMenu);
		menuBar.add(helpMenu);
		
		return menuBar;
	}
	
	private static void configureDocking(Frame owner) {
		// setup the DockingManager to work with our application
		DockingManager.setDockableFactory(new ViewFactory(owner));
		DockingManager.setFloatingEnabled(true);		
		// configure the perspective manager
		PerspectiveManager.setFactory(new DemoPerspectiveFactory());
		PerspectiveManager.setRestoreFloatingOnLoad(true);
		PerspectiveManager mgr = PerspectiveManager.getInstance();
		mgr.setCurrentPerspective(P1, true);
        
		// load any previously persisted layouts
		PersistenceHandler persister = FilePersistenceHandler.createDefault(PERSPECTIVE_FILE);
		PerspectiveManager.setPersistenceHandler(persister);
		try {
			DockingManager.loadLayoutModel();
		} catch(IOException e) {
			e.printStackTrace();
		} catch (PersistenceException e) {
            e.printStackTrace();
        }
		// remember to store on shutdown
		DockingManager.setAutoPersist(true);
	}
	
	private static class DemoPerspectiveFactory implements PerspectiveFactory {
		
		public Perspective getPerspective(String persistentId) {
			if(P1.equals(persistentId))
				return createPerspective1();
			if(P2.equals(persistentId))
				return createPerspective2();			
			return null;
		}
		
		private Perspective createPerspective1() {
			Perspective perspective = new Perspective(P1, "Explorer");
			LayoutSequence sequence = perspective.getInitialSequence(true);
			
//			sequence.add(MAIN_VIEW);
//			sequence.add(EXPLORER_VIEW, MAIN_VIEW, EAST_REGION, .3f);
//			sequence.add(DATASTORE_VIEW, MAIN_VIEW, WEST_REGION, .3f);
//			sequence.add(MAP_VIEW, DATASTORE_VIEW);
			sequence.add(EXPLORER_VIEW, DATASTORE_VIEW);
			
			return perspective;
		}

		private Perspective createPerspective2() {
			Perspective perspective = new Perspective(P2, "Mapping");
			LayoutSequence sequence = perspective.getInitialSequence(true);
			
			sequence.add(MAP_VIEW);
			sequence.add(DATASTORE_VIEW, MAP_VIEW, NORTH_REGION, .3f);
			//sequence.add(MAP_VIEW, DATASTORE_VIEW);
			
			return perspective;
		}
		
		
		
	}
	
	private static class ViewFactory extends DockableFactory.Stub {
		
		public ViewFactory(Frame owner) {
			this.owner = owner;
		}
		
		Frame owner;
		
		public Component getDockableComponent(String dockableId) {			
			if(EXPLORER_VIEW.equals(dockableId))
				return createView(EXPLORER_VIEW, "Explorer", "birdView001.png", owner);
			if(DATASTORE_VIEW.equals(dockableId))
				return createView(DATASTORE_VIEW, "DataStore", "msgLog001.png", null);
			if(MAP_VIEW.equals(dockableId))
				return createView(MAP_VIEW, "Mapping", "problems001.png", null);
			if(CONSOLE_VIEW.equals(dockableId))
				return createView(CONSOLE_VIEW, "Console", "console001.png", null);
			return null;
		}
		
		private View createView(String id, String text, String iconName, Frame owner) {
			
			View view = new View(id, text);
			//Dodajemy akcje close to tego view
			view.addAction(DockingConstants.CLOSE_ACTION);
			view.addAction(DockingConstants.PIN_ACTION);
			
			JPanel panel;
			if(id.equals(EXPLORER_VIEW)) {
				panel = new ExplorerPanel(owner);
			}
			else {
				panel = new JPanel();
				panel.setBorder(new LineBorder(Color.GRAY, 1));
				
				JTextField textField = new JTextField(text);
				textField.setPreferredSize(new Dimension(100, 20));
				panel.add(textField);				
			}

			view.setContentPane(panel);
			
			Icon icon = ResourceManager.createIcon("org/flexdock/demos/view/" + iconName);
			view.setTabIcon(icon);

			return view;
		}
		
		private static View createMainView() {
			
			JTabbedPane tabbedPane = new JTabbedPane();
			tabbedPane.addTab("Sample1", new JTextArea("Sample1"));
			tabbedPane.addTab("Sample2", new JTextArea("Sample2"));
			tabbedPane.addTab("Sample3", new JTextArea("Sample3"));
			
//			//to view nie bedzie mialo tytulu, wiec przekazujemy null
//			View mainView = new View(MAIN_VIEW, null, null);
//
//			//blokujemy mozliwosc dokowania do tego view w regionie CENTER
//			mainView.setTerritoryBlocked(CENTER_REGION, true);
//			//wylaczamy pasek tytulowy
//			mainView.setTitlebar(null);
//			//ustawiamy komponent GUI, ktory chcemy aby byl wyswietalny w tym view
//			mainView.setContentPane(new JScrollPane(tabbedPane));
			
			return null;
		}
	}
	
}


